package com.ask.service.businessService.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ask.dto.Result;
import com.ask.entity.ShopType;
import com.ask.mapper.ShopTypeMapper;
import com.ask.service.businessService.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 查询所有店铺类型信息
     *
     * @return
     */
    @Override
    public Result listWithCache() {
        // 缓存存在直接返回
        String key = "cache:shopType";
        String cacheShopType = stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(cacheShopType))
            return Result.success(JSONUtil.toList(cacheShopType, ShopType.class));
        // 缓存为空查询数据库
        List<ShopType> list = query().orderByAsc("sort").list();
        // 缓存存储至redis
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(list));
        // 返回店铺类型信息
        return Result.success(list);
    }
}
